<?php
/* @var $this ProductController */

$this->breadcrumbs=array(
	'Product',
);
?>
<div class="wap-search">      
   <div class="form-search" id="<?php echo Yii::app()->controller->id . '-' . Yii::app()->controller->action->id?>">
      <?php $form=$this->beginWidget('CActiveForm', array(
      	'id'=>Yii::app()->controller->id . '_' . Yii::app()->controller->action->id,
      	'enableAjaxValidation'=>false,
         'enableClientValidation'=>true
      )); ?>
      
      
      	<?php echo $form->errorSummary($model,'Kiểm tra lại các giá trị bạn vừa nhập vào'); ?>
         <input name="session_user" type="hidden" value="<?php echo $session_user?>"/>
         <input name="action_user" type="hidden" value="search" id="action_user" />
            <table>
               <tr>
                  <td>
                     Từ ngày<br />
                     <?php 
                        $start_date = date('d/m/Y', strtotime('-1 week'));     
                        echo CHtml::textField('start_date',$start_date,array('class' => 'datepicker','style' => 'width:150px;padding:0.5px 3px;')); 
                     ?>
                     
                  </td>
                  <td>
                     Đến ngày<br />
                     <?php 
                        $end_date = date('d/m/Y', time()); 
                        echo CHtml::textField('end_date',$end_date,array('class' => 'datepicker','style' => 'width:150px;padding:0.5px 3px;'));
                     ?>
                  </td>
                  <td>
                     Trạng thái<br />
                     <?php $model->ord_status = (is_null($model->ord_status)) ? 2 : $model->ord_status ?>
                     <?php echo $form->dropDownList($model,"ord_status",array("2" => 'Tất cả','1' => 'Đã giao hàng','0' => 'Chưa giao hàng'),array('style' => 'width:150px'))?>
                  </td>
                  <td>
                  Tìm kiếm<br />
                  <input class="btn" type="submit" value="Tìm kiếm" /></td>
               </tr>
            </table>
      <?php $this->endWidget(); ?>
   </div> 
</div>
<div>
   <div id="grid">
      <table>
         <tr class="grid-header">
            <th class=""><input class="row-check-all" type="checkbox" id="chk_SelectAllRow"/></th>
            <th style="width:36px;"><div>STT</div></th>
            <th>ID</th>
            <th>Thời gian</th>
            <th>Thông tin KH</th>
            <th>Tên sản phẩm</th>
            <th>SL</th>
            <th>Đơn giá</th>
            <th>Giảm giá</th>
            <th>Thành tiền</th>
            <th>Trạng thái</th>
         </tr>
        <?php if(count($list) > 0){
         //CVarDumper::dump($list,10,true);die();
         $i = 0;
         foreach($list as $item){
            $count_product = count($item['products']);
            $i++;
            $total_money_item = 0;
            $k = 0;
            foreach($item['products'] as $product_item){
               
               $k++;
               $product_info = Books::model()->findByPk($product_item['ord_id_product']);
               if(!is_null($product_info)){
               $money = $product_info->book_price * ((100 - $product_info->book_sale)/100) * ($product_item['ord_qty_product']); 
               $sale = ($product_info->book_price * $product_item['ord_qty_product']) - $money;
               $total_money_item += $money;
               ?>
               <tr class="grid-row-<?php echo $i%2?>">
                  <?php if($k == 1){?>
                     <td rowspan="<?php echo $count_product?>" class="select-row text-center">
                        <input class="row-check" type="checkbox" value="<?php echo $item['ord_id']?>"/>
                     </td>
                     <td rowspan="<?php echo $count_product?>" align="center"><?php echo $i?></td>
                     <td rowspan="<?php echo $count_product?>"><?php echo $item['ord_id']?></td>
                     <td rowspan="<?php echo $count_product?>"><?echo date('H:i - d/m/Y',$item['ord_date']);?></td>
                     <td rowspan="<?php echo $count_product?>">
                        <?php 
                           echo '<strong>Tên/Email: </strong>' . $item['ord_name_customer'].'<br />';
                           echo '<strong>Địa chỉ:</strong> ' . $item['ord_add_customer'].'<br />';
                           echo '<strong>SĐT: </strong>' . $item['ord_phone_customer'].'<br />';
                        ?>
                     </td>
                  <?php }?>
                  <td>
                     <?php echo $product_info->book_name?>
                  </td>
                  <td>
                     <?php echo $product_item['ord_qty_product']?>
                  </td>
                  <td>
                     <?php echo Str::formatNumber($product_info->book_price) . ' đ'?>
                  </td>
                  <td>
                     <?php echo Str::formatNumber(round($sale,0)) . ' đ'?>
                  </td>
                  <td><?php echo Str::formatNumber(round($money,0)) . ' đ' ;?></td>
                  <?php if($k == 1){?>
                  <?php 
                     if($item['ord_status'] == 1){
                        $text = 'Đã giao hàng';
                        $class = 'isa_success';
                     }else{
                        $text = 'Chưa giao hàng';
                        $class = 'isa_warning';
                     }
                  ?>
                  <td rowspan="<?php echo $count_product?> style="text-align: center;" class="<?php echo $class?>">
                     <?php echo $text?>
                  </td>
                  <?php }?>
               </tr>
            <?php }else{?>
            <tr>
               <td rowspan="<?php echo $count_product?>" class="select-row text-center">
                  <input class="row-check" type="checkbox" value="<?php echo $item['ord_id']?>"/>
               </td>
               <td colspan="7" style="color: #cc0000; text-align: center;">
                  Sản phẩm không tồn tại hoặc đã bị xóa !.
               </td>
            </tr>
            <?php }?>
            <?php }?>
            <tr>
               <td colspan="9" style="text-align: right;">Tổng tiền hóa đơn</td>
               <td colspan="2"><span class="highlight bold"><?php echo  Str::formatNumber($total_money_item) . ' đ'?></span></td>
            </tr>
            <?php } ?>
         <?php }?>
       
      </table>
      <form class="form" id="option_form" action="" method="POST">
         <table>
            <tr>
               <td>  
                  Số dòng:
                  <select name="limit" id="limit">
                     <?foreach($rowList as $value){?>
                        <option value="<?php echo $value?>" <?php if($value == $limit) echo 'selected'?> > <?php echo $value?></option>
                     <?}?>
                  </select>
               </td>
               <td class="text-center">
                  Trang:
                  <input id="page_input" name="page" type="text" value="<?php echo $page?>" />
                  Tổng số:
                  <?php echo $total_pages?>
               </td>
               <td class="text-right">
                  Xem:
                     <?php echo $start + 1?>
                      - 
                     <?php 
                     $end = $limit*$page;
                     $end = ($end > $total_record) ? $total_record : $end;
                     echo $end ?>
                  Tổng số:
                     <?php echo $total_record?>
               </td>
            </tr>
         </table>
      </form>
   </div>
</div>
<script>
   $(document).ready(function(){
      $(".datepicker").datepicker({
         dateFormat:"dd/mm/yy",
         defaultDate: '<?php echo $start_date?>',
      });
      
      //Select all record
      $("#chk_SelectAllRow").click(function(){
         var checkboxes = $(".row-check");
         if($(this).is(':checked')) {
            checkboxes.attr('checked', 'checked');
         } else {
            checkboxes.removeAttr('checked');
         }
      });
      
      //Submit form when limit change
      $("#limit").change(function(){
         $("#option_form").submit();
      });
      
      //Submit form when page change
      var old_page;
      var new_page;
      $("#page_input").focus(function(){
         old_page = $(this).val();
      });
      $("#page_input").blur(function(){
         new_page = $(this).val();
         if(new_page !== old_page) $("#option_form").submit();
      });
      
      //Deactive record
      $("#deactive").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            console.log(data);
            var url = '<?php echo CController::createUrl('deactive')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Cập nhật thành công " + data.record_affected + " bản ghi.");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Active record
      $("#active").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('active')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Cập nhật thành công " + data.record_affected + " bản ghi.");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Delete record
      $("#delete").click(function(){
         var list_id = [];
         $("input.row-check").each(function(){
            if($(this).is(":checked")){
               list_id.push($(this).val());
            }
         });
         if(list_id.length === 0){
            alert("Bạn phải chọn ít nhât 1 bản ghi");
         }else{
            var data = {'list_id':list_id,'session_user':'<?php echo $session_user?>'};
            var url = '<?php echo CController::createUrl('delete')?>';
            var succ = function(data){
               if(data.error == 0){
                  alert("Có " + data.record_affected + " bản ghi bị xóa");
                  location.reload();
               }
            }
            var _confirm = confirm("Bạn có chắc chắn thực hiện thao tác này ?");
            if(_confirm)
               handleAjax(url,"POST","json",data,succ);
         }
         
      });
      
      //Edit record 
      var id_edit = 0;
      $(".row-check").click(function(){
         id_edit = $(this).val();
      });
      $("#edit").click(function(){
         if(id_edit == 0){
            alert("Bạn phải chọn 1 bản ghi để sửa")
         }else{
            window.location.href = '<?php echo CController::createUrl('edit')?>' + '/' + id_edit;
         }
      });
      
      //Refresh record 
      $(".refresh-record").click(function(){
         var id = $(this).attr('data-id');
         data = {'id':id};
         bef = function(){
            $(this).parent().addClass('loading');
         }
         succ = function(){
            $(this).parent().removeClass('loading');
         }
         handleAjax('/admin/post/refresh','POST','json',data);
         var checked = $(this).is(":checked");
         console.log(checked);
         if(checked){
            status = 1;
         }
      })
      
      // Check/uncheck VIP
      $(".vip-record").click(function(){
         var id = $(this).attr('data-id');
         var vip;
         bef = function(){
            $(this).parent().addClass('loading');
         }
         succ = function(){
            $(this).parent().removeClass('loading');
         }
         var checked = $(this).is(":checked");
         if(checked){
            vip = 1;
         }else{
            vip = 0;
         }
         data = {'id':id,'vip':vip};
         handleAjax('/admin/post/vip','POST','json',data);
      })
         
         
   });
</script>